package likouJava;

public class 反转链表 {

    //双指针
//    public ListNode reverseList(ListNode head) {
//        ListNode prev = null;    //前一个节点
//        ListNode curr = head;    //当前节点
//
//        while (curr != null){
//            ListNode next = curr.next; //保存下一个节点
//            curr.next = prev;          //当前节点指向前一个
//            prev = curr;               //prev前移
//            curr = next;               //cur前移
//        }
//        return prev;
//    }

    //递归
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null){
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return  newHead;
    }

}
